﻿using System;
using UnityEngine;

public class DialogBaseCtrl : CSIMediator
{
    private DialogBasePanel dialogBase;
    DialogBaseView view = new DialogBaseView();
    private ObjData dialogData;
    
    public override void SetData(params object[] args)
    {
        dialogData = (ObjData)args[0];
    }
    
    public override void Awake()
    {
        base.Awake(); view.Init(Go);
        dialogBase = view.dialogBase;
    }

    public override void Open()
    {
        base.Open();
        view.Refresh(dialogData);
    }

    public override void Close()
    {
        base.Close();
    }

    public override void Destroy()
    {
        dialogBase = null;
        dialogData = null;

        view.Clear();
        view = null;
        base.Destroy();
    }

    public override void AddListener()
    {
        UIEventListener.Get(dialogBase.Close_obj).onClick = OnClick;
        UIEventListener.Get(dialogBase.Dialog1_obj).onClick = OnClick;
        UIEventListener.Get(dialogBase.Dialog2_obj).onClick = OnClick;
    }

    public override int PanelId
    {
        get { return (int)PanelID.DialogBasePanel; }
    }
    
    private void OnClick(GameObject go)
    {
        Close(); //关闭界面
        if (go == dialogBase.Dialog1_obj)
        {
            ClickCallBack(0);
        }
        else if (go == dialogBase.Dialog2_obj)
        {
            ClickCallBack(1);
        }
    }

    private void ClickCallBack(int idx)
    {
        if (dialogData.callArg != null)
        {
            Action callBack = dialogData.callArg[idx];
            if (callBack != null) { callBack(); }
        }
    }
}